package com.huilian.iotbox.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huilian.iotbox.admin.dao.LedgerOrderDao;
import com.huilian.iotbox.admin.service.LedgerOrderService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.LedgerOrderDto;
import com.huilian.iotbox.data.enums.LedgerOrderLedgerTypeEnum;
import com.huilian.iotbox.data.enums.OrderStatusEnum;
import com.huilian.iotbox.data.vo.LedgerOrderVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class LedgerOrderServiceImpl implements LedgerOrderService {
    @Resource
    private LedgerOrderDao ledgerOrderDao;

    @Override
    public PageInfo findLedgerOrderList(LedgerOrderDto ledgerOrderDto) {
        PageHelper.startPage(ledgerOrderDto.getPageNum(), ledgerOrderDto.getPageSize(), false);
        List<LedgerOrderVo> memberList = ledgerOrderDao.findLedgerOrderList(ledgerOrderDto);
        PageInfo pagResult = new PageInfo(memberList);
        if (memberList.size() < 10) {
            pagResult.setTotal(ledgerOrderDto.getPageSize() * ledgerOrderDto.getPageNum());
        } else {
            pagResult.setTotal(ledgerOrderDto.getPageSize() * ledgerOrderDto.getPageNum() + 1);
        }
        return pagResult;
    }

    @Override
    public ServerResponse findStat(LedgerOrderDto ledgerOrderDto) {
        LedgerOrderVo ledgerOrderVo = ledgerOrderDao.findStat(ledgerOrderDto);
        return ServerResponse.createBySuccess(ledgerOrderVo);
    }

    @Override
    public BigDecimal getTiktokMeituanServiceFeeByUserId(Integer userId, String yesterdayStartTime, String yesterdayEndTime) {
        LedgerOrderDto ledgerOrderDto = new LedgerOrderDto();
        ledgerOrderDto.setUserId(userId);
        ledgerOrderDto.setStartTime(yesterdayStartTime);
        ledgerOrderDto.setEndTime(yesterdayEndTime);
        ledgerOrderDto.setStatus(OrderStatusEnum.SUCCESS.getCode());
        ledgerOrderDto.setLedgerType(LedgerOrderLedgerTypeEnum.TIKTOK_MEITUAN_CANCELLATION_FEE.getCode());
        return ledgerOrderDao.getLedgerOrderMoneyByLedgerType(ledgerOrderDto);
    }
}
